[转载] i卡也能跑AI画图?Stable Diffusion with Intel Arc GPU运行实战 您所在的位置:网站首页 显卡k6000 12g能ai绘画吗 [转载] i卡也能跑AI画图?Stable Diffusion with Intel Arc GPU运行实战

[转载] i卡也能跑AI画图?Stable Diffusion with Intel Arc GPU运行实战

2024-07-01 23:46| 来源: 网络整理| 查看: 265

2023.2.22编辑:已经有大佬写了适配i卡的webui,指路 

https://github.com/Aloereed/stable-diffusion-webui-ipex-arc(B站同id)

本文章转载自英特尔员工2023年2月3日的帖子:

[https://medium.com/intel-analytics-software/stable-diffusion-with-intel-arc-gpus-f2986bba8365]

参考文档:

https://intel.github.io/intel-extension-for-pytorch/xpu/latest/tutorials/installation.html

https://github.com/intel/intel-extension-for-pytorch

    “The Intel Extension for PyTorch”项目(ipex)通过在PyTorch官方注册的“device = xpu”设备为英特尔独立 GPU 提供深度学习加速。

    这允许用户在Ubuntu平台,或者在WIndows10 21H2/Win11平台依托WSL2或者Docker for windows,来配置显卡加速pytorch训练和推理。

  (和AMD显卡的ROCm相比,后者只能运行在ubuntu等Linux系统上,但是仍然可以使用device = cuda设备代码,无须修改源代码,更加友好)

    Docker Desktop有点类似于windows平台的虚拟机包和镜像管理系统,它基于WSL2开发,因此和WSL2一样具有VScode远程开发等功能。通过拉取Docker Hub提供的官方镜像可以简化环境依赖安装,非常方便。

    本文将会使用Intel Arc A770 独显运行一个最基础的Stable Diffusion AI text-image模型。

步骤

环境依赖

    首先要保证安装了正确的Arc显卡驱动(参见官方文档的安装说明)

    确认系统已升级到Win11 或最新的Win10,以获得完整的WSL2功能。

安装基于WSL2的 windows Docker

    首先要启用WSL2功能,这部分不是重点,网上教学非常丰富。注意这一步和安卓模拟器冲突,只能选一个

    

wsl -l -v

    确认WSL2正常启用后,在Docker官网安装客户端并重启,确认windows Docker启动成功,需要对Docker的操作有一定了解。

    (如果不想用Docker的话,可以参考官方文档,在wsl2 ubuntu或Ubuntu物理机下手动安装ipex及其依赖环境,道理都是一样的)

拉取官方ipex镜像

    官方提供的ipex镜像可以通过 [hub.docker.com/r/intel/intel-extension-for-pytorch/tags] 访问,Arc独显用户理论上应该使用gpu这个镜像,但也可以试试latest。镜像中已经包含了OneAPI Toolkit等环境依赖,比较方便。在启动Docker容器之前,先把SD离线模型下载下来!

mkdir c:\data

cd c:\data

git lfs install

git clone https://huggingface.co/runwayml/stable-diffusion-v1-5

(具体路径请自行修改)

    运行官方镜像启动一个容器(注意这里原作者留了一个端口给Jupter Notebook开发,但我更喜欢用VScode,功能非常强大。用户可以自行决定哪一个更适合)

docker run -it --rm

                --device /dev/dxg

                -v /usr/lib/wsl:/usr/lib/wsl

                -v c:\data:/data

                -p 9999:9999

                intel/intel-extension-for-pytorch:gpu

    通过python命令行检查docker是否连接到A770显卡。并通过声明的9999端口启动Jupyter Notebook远程连接

python -c "import torch;import intel_extension_for_pytorch as ipex;print(ipex.xpu.get_device_name(0))"

cd c:/data

pip install -q jupyter

jupyter notebook --allow-root --ip 0.0.0.0 --port 9999

    这里应该能够正确识别显卡

用Arc A770 显卡运行Stable Diffusion text2image模型

    用浏览器打开Jupyter给的URL来进行开发。运行SD模型。

# install Stable Diffusion for this notebook

%pip install -q diffusers transformers accelerate

 

import intel_extension_for_pytorch as ipex

import torch

from diffusers import StableDiffusionPipeline

# check Intel GPU

print(ipex.xpu.get_device_name(0))

# load the Stable Diffusion model

pipe = StableDiffusionPipeline.from_pretrained("./stable-diffusion-v1-5",

                                               revision="fp16",

                                               torch_dtype=torch.float16)

# move the model to Intel Arc GPU

pipe = pipe.to("xpu")

# model is ready for submitting queries

pipe("an astronaut riding a horse on mars").images[0]

# run another query

pipe("cat sitting on a park bench").images[0]

    可以成功得到结果,收工

    同样,用户可以通过这个项目在WSL2 和 Intel Arc GPU 上运行任何其他的 PyTorch 模型,并且通过官方提供的ipex.optimize函数对运行速度进行优化。

    目前大部分AI绘画的前端脚本都是基于CUDA进行编写,因此兼容ipex xpu接口的webui仍有待开发。

    关键词:webui novelai ai绘图 ai绘画 英特尔显卡 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有